

$(function() {

    let info = null

    const id = getCookie('goods_id')
    console.log(id)

    getGoodsInfo()

    async function getGoodsInfo() {

        const goodsInfo = await $.get('../php/getInfo.php',{goods_id : id}, null , 'json')

        bindHtml(goodsInfo.info)
        new Enlarge('#box1')

        info = goodsInfo.info
        
    }

    function bindHtml(info) {

        // console.log(info)

        // str=`<p></p>`
        $('.product_container  h3').html(`<p>${ info.goods_name }</p>`)

        $('.product-side').html(`
        <div class="big_imgBox">
            <img src="${ info.goods_big_logo }" alt="">
            <div class="mask"></div>
        </div>
        <div class="small_imgBox">
            <p class="active">
                <img src="${ info.goods_small_logo }" alt="">
            </p>
            <p>
                <img src="${ info.goods_small_logo }" alt="">
            </p>
        </div>
        <div class="enlarge" style="background-image: url(${ info.goods_big_logo }"></div>
        `)

        $('.product').html(`
        <div class="top_top">
            <p>
                销售价:
            </p>
            <h2>
                ￥${ info.goods_price }
            </h2>
        </div>
        <div class="detail_one">
            <p>商品评分：</p>
        </div>
        <div class="product-params">
            <p>货品编号：</p>
            <p>${ info.add_time }</p>
        </div>
        <div class="product-buy">
            <div class="item-content">
                <p>
                    <span>
                        数量：
                    </span>
                </p>
                <button class="subNum">-</button>
                <input type="text" value="1" class="cartNum">
                <button class="addNum">+</button>
            </div>
            <div class="product-buy-action">
                <p>立即购买</p>
                <p class="p_index">加入购物车</p>
            </div>
        </div>
        `)

        // $('.goodsDesc').html(info.goods_introduce)

    }

    $('.product').on('click','.p_index',function() {
        console.log(123)

        const cart = JSON.parse(window.localStorage.getItem('cart')) || []

        const flag = cart.some(item => item.goods_id === id)

        if (flag) {
       
        const cart_goods = cart.filter(item => item.goods_id === id)[0]
       
        cart_goods.cart_number = cart_goods.cart_number - 0 + ($('.cartNum').val() - 0)
      } else {

        info.cart_number = $('.cartNum').val() - 0

        cart.push(info)
      }

        window.localStorage.setItem('cart', JSON.stringify(cart))

        // window.location.reload()
        // fn()
    })

    // function fn(){

    //     const num = getCookie('total') 
    //     console.log(1231323)
    //     str =`<b>${ num }</b>`
    //     $('.clearfix > p > span').html(str)

    // }

    $('.product')
    .on('click','.subNum',function(){
        let num = $('.cartNum').val() - 0
        if (num === 1) return
        $('.cartNum').val(num - 1)
    })
    .on('click', '.addNum', function () {
        // 拿到 input 的 value 值
        let num = $('.cartNum').val() - 0
        // 否则就进行 -- 操作, 然后在设置回去
        $('.cartNum').val(num + 1)
      })
    })
